<template>
  <div class="bottom">
    <el-form label-width="120px">
      <el-form-item label="底部版权">
        <div>
          <el-radio-group v-model="setCopyright" @change="handleCopyright">
            <el-radio
              v-for="item in copyrightList"
              :key="item.name"
              :label="item.value"
              >{{ item.name }}</el-radio
            >
          </el-radio-group>
          <div v-if="setCopyright == 'CUSTOMIZE'">
            <el-input
              style="width: 300px"
              clearable
              v-model="details.customizeCopyright"
              placeholder="请输入"
              @change="handleCustom"
            ></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="底部菜单">
        <el-checkbox-group v-model="selected">
          <el-checkbox
            @change="handleNav($event, index)"
            v-for="(item, index) in navList"
            :label="item.name"
            :key="item.name"
            >{{ item.name }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="设置首页">
        <el-radio-group v-model="setHome" @change="handleSetHome">
          <el-radio
            v-for="item in navList"
            :key="item.name"
            :label="item.name"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navList: [],
      selected: [],
      setHome: "",
      details: {},
      copyrightList: [],
      setCopyright: "",
    };
  },
  created() {
    this.getNave();
    this.getbasicPage();
    this.getcopyright();
  },
  methods: {
    handleCustom() {
      console.log(this.custom);
      this.$api("vote.savebasicPage", {
        ...this.details,
        copyright: this.setCopyright,
        voteId: this.$route.query.id,
      }).then((res) => {
        console.log(res);
        this.$store.commit("status/SET_URL", new Date().getTime());
        this.$message({
          message: "保存成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    getcopyright() {
      this.$api("vote.copyright").then((res) => {
        console.log(res);
        this.copyrightList = res;
      });
    },
    getbasicPage() {
      this.$api("vote.getbasicPage", {
        voteId: this.$route.query.id,
      }).then((res) => {
        this.details = res;
        this.setCopyright = res.copyright;
      });
    },
    copyrightClick(i) {
      this.current = i;
    },
    getNave() {
      this.$api("page.getNave", {
        voteId: this.$route.query.id,
      }).then((res) => {
        console.log(res);
        this.navList = res.nav;
        this.navList.forEach((item) => {
          if (item.enabled) {
            this.selected.push(item.name);
          }
          if (item.home) {
            this.setHome = item.name;
          }
        });
      });
    },
    saveApi() {
      this.$api("page.saveNav", {
        nav: this.navList,
        voteId: this.$route.query.id,
      }).then((res) => {
        this.$store.commit("status/SET_URL", new Date().getTime());
        console.log(res);
        this.$message({
          message: "保存成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    handleNav(val, index) {
      this.navList[index].enabled = val;
      this.saveApi();
    },
    handleSetHome() {
      this.navList.forEach((item) => {
        item.home = false;
        if (this.setHome == item.name) {
          item.home = true;
        }
      });
      console.log(this.navList);
      this.saveApi();
    },
    handleCopyright() {
      console.log(this.setCopyright);
      this.$api("vote.savebasicPage", {
        ...this.details,
        copyright: this.setCopyright,
        voteId: this.$route.query.id,
      }).then((res) => {
        console.log(res);
        this.$store.commit("status/SET_URL", new Date().getTime());
        this.$message({
          message: "保存成功",
          type: "success",
          duration: 1000,
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.bottom {
  margin: 30px 0 0 50px;
  width: 900px;
  ::v-deep.el-form-item__label {
    padding-right: 40px;
  }
  .outerBox {
    width: 200px;
    height: 40px;
    display: flex;
    align-items: center;
    .showBox {
      border-radius: 30px;
      background-color: #dbdbdb;
      width: 100%;
      height: 30px;
      display: flex;
      color: #fff;
      font-weight: 600;
      .active {
        background-color: $themeColor;
      }
      .copyright {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 50%;
        height: 100%;
        border-radius: 30px;
        cursor: pointer;
      }
    }
  }
  span {
    margin-left: 10px;
    color: $deactiveColor;
  }
}
</style>